package br.com.exceptiongroup.chatexception.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Controller;

import br.com.exceptiongroup.chatexception.model.Usuario;
import br.com.exceptiongroup.chatexception.service.UsuarioService;

@Controller
@Scope(value="request")
public class LoginMB extends AbstractManagedBean implements UserDetailsService {

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 5256084054596220593L;
	
	@Autowired
	private UsuarioService usuarioService;

	@Override
	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException {
		
		//TODO implementar busca no banco
		Usuario usuario = (Usuario) usuarioService.loadUserByUsername(username);
		
		if(usuario == null) {
			throw new BadCredentialsException("usuario invalido");
		}
		
		return usuario;
	}

}
